package com.example.demo.controller;

import com.example.demo.aop.Log;
import com.example.demo.aop.Login;
import com.example.demo.config.GetBeanUtil;
import com.example.demo.config.RedisService;
import com.example.demo.exceptionhandler.BusinessException;
import com.example.demo.exceptionhandler.EmBusinessError;
import com.example.demo.model.SysRole;
import com.example.demo.responsehandler.ResponseUtil;
import com.example.demo.service.PrintService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.*;
import java.util.concurrent.*;

/**
 * @author whn
 */
@RestController
@RequestMapping("/test")
@CrossOrigin
@Api(value = "测试接口，请忽略", tags = {"测试接口，请忽略"})
public class TestController {
    @Autowired
    private PrintService printService;

    private final int SIZE = 53;

    RedisService redisService = (RedisService) GetBeanUtil.getBean("getRedisService");

    @GetMapping("/get")
    public Map<String, String> get() {
        HashMap<String, String> map = new HashMap<>(1);
        map.put("data", "2");
        return map;
    }

    @GetMapping("/setName")
    public void setName(@RequestParam("name") String name) throws IOException {
        if (name.equals("")) {
            throw new BusinessException(EmBusinessError.DISHIES_TYPE, "kong");
        }
        redisService.set("name", name);
        ResponseUtil.showMessage("yes", "yes", "yes");
    }

    @GetMapping("/getName")
    @Login
    public Map<String, Object> getName(SysRole sysRole) throws InterruptedException {
        List<Object> list = Collections.synchronizedList(new ArrayList<>());
        for (int i = 0; i < SIZE; i++) {
            list.add(redisService.get("name"));
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return ResponseUtil.toMessage("yes", "yes", list);
    }

    @GetMapping("/thread")
    @ApiOperation(value = "多线程处理接口")
    @Login
    @Log(operation = "nihao",type = 2)
    public Map<String, Object> thread(SysRole sysRole) {
        List<Object> list = Collections.synchronizedList(new ArrayList<>());
        ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) GetBeanUtil.getBean("executor");

        CountDownLatch countDownLatch = new CountDownLatch(SIZE);
        for (int i = 0; i < SIZE; i++) {
            threadPoolExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    list.add(Thread.currentThread().getName() + "取得的结果为：" + redisService.get("name"));
                    countDownLatch.countDown();
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Finished all threads");
        return ResponseUtil.toMessage("yes", "yes", list);
    }
}
